import {getStore, setStore} from '@/libs/utils/store'
import {listHolidayByYear} from '@/projects/basic/api/admin/holiday'
const app = {
    state: {
        pubKey: '',
        tenantId: getStore({
            name: 'tenant_id'
        }) || 'system',
        // 在这里定义你不想要缓存的页面的name属性值(参见路由配置router.js)
        dontCache: ['login'],
        hasLogin:false,
        keepRef: {},
        scrollTop: {},
        holidayInfo:{},
        batchDate:'',//批量日期,日历初始日期
        mainComponent:null
    },
    actions: {
        updatePubKey({commit}, pubkey) {
            return new Promise(resolve => {
                commit('updatePubKey', pubkey)
            })
        },
        updateTenantId({commit}, tenantId) {
            return new Promise(resolve => {
                commit('setTenantId', tenantId)
            })
        },
        updateScroll({commit}, positionInfo) {
            commit('setScrollTop', positionInfo)
        },
        cacheYearHoliday({commit}, year) {
            return new Promise((resolve) => {
                listHolidayByYear(year).then(res=>{
                    if(res.code==='0000'){
                        resolve(res.data);
                    }else{
                        resolve({});
                    }
                }).catch(e=>{
                    resolve({});
                })
            })
        },
    },
    mutations: {
        updatePubKey: (state, pubkey) => {
            state.pubKey = pubkey;
        },
        setHasLogin:(state,status)=>{
            state.hasLogin = status;
        },
        updateRef: (state, ref) => {
            state.keepRef = ref;
        },
        setTenantId(state, tenantId) {
            state.tenantId = tenantId;
            setStore({
                name: 'tenant_id',
                content: state.tenantId,
                type: 'session'
            })
        },
        setScrollTop(state, {routerName, position}) {
            state.scrollTop[routerName] = position;
        },
        setMainComponent(state, main) {
            state.mainComponent = main;
        },
        cleanKeepAlive(state, obj) {
            state.keepRef.removeCacheByKey(obj.key);
        },
        cleanAllKeepAlive(state, obj) {
            state.keepRef.removeAllCache();
        },
        cleanOthersKeepAlive(state, obj) {
            state.keepRef.removeOtherCache(obj.key);
        },
        updateBatchDate(state,batchDate){
            state.batchDate = batchDate;
        }
    }
};

export default app;
